import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:lai_zhan/common/redux/AppState.dart';
import 'package:lai_zhan/common/dao/UserDao.dart';
import 'package:lai_zhan/common/style/AppStyle.dart';
import 'package:lai_zhan/common/util/NavigatorUtil.dart';
import 'package:lai_zhan/widget/FlexButton.dart';

class WelcomePage extends StatefulWidget {
  static final String rName = "/";

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new WelcomePageState();
  }
}

class WelcomePageState extends State<WelcomePage> {
  bool hadInit = false;
  bool visible = true;

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    if (hadInit) return;
    hadInit = true;
    Store<AppState> store = StoreProvider.of(context);
    UserDao.initUserInfo(store).then((res) {
      if (res != null && res.result) {
        //toHome
      } else {
        //toLogin
        //NavigatorUtil.goLogin(context);
        new Future.delayed(Duration(seconds: 3), () {
          setState(() {
            this.visible = false;
          });
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return StoreBuilder<AppState>(builder: (context, store) {
      return new Column(
        children: <Widget>[
          new Expanded(
            flex: 1,
            child: new Container(
                alignment: Alignment.bottomCenter,
                padding: EdgeInsets.only(left: 60, right: 60, bottom: 60),
                decoration: BoxDecoration(
                    color: Color(AppColors.white),
                    image: new DecorationImage(
                        image: AssetImage('lib/images/cover_image.png'))),
                child: new Offstage(
                  offstage: this.visible,
                  child: new Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      new FlexButton(
                        text: '我有账号',
                        color: Color(AppColors.primaryColor),
                        textColor: Color(AppColors.white),
                        onPress: () {
                          NavigatorUtil.goLogin(context);
                        },
                      ),
                      new Padding(padding: EdgeInsets.all(5)),
                      new FlexButton(
                        text: '我是新用户',
                        color: Color(AppColors.primaryColor),
                        textColor: Color(AppColors.white),
                        onPress: () {
                          NavigatorUtil.goRegister(context);
                        },
                      ),
                    ],
                  ),
                )),
          )
        ],
      );
    });
  }
}
